﻿using System;
using System.Collections.Generic;
using System.IO;
using Assets.Scripts.DynamicClouds;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;

// Token: 0x0200035C RID: 860
public class SceneLoader : MonoBehaviour
{
	// Token: 0x0600163D RID: 5693 RVA: 0x00090618 File Offset: 0x0008E818
	private void Awake()
	{
		if (UnityEngine.Object.FindObjectsOfType<SceneLoader>().Length > 1)
		{
			UnityEngine.Object.DestroyImmediate(this);
			return;
		}
		UnityEngine.Object.DontDestroyOnLoad(this);
		SceneManager.sceneLoaded += this.OnSceneLoaded;
		this.loadedAssetBundles = new Dictionary<string, string>();
		this.shaderData = new Dictionary<string, string>();
	}

	// Token: 0x0600163E RID: 5694 RVA: 0x00090668 File Offset: 0x0008E868
	public List<SceneLoader.SceneInfo> GetSceneList()
	{
		if (this.allScenes == null)
		{
			this.allScenes = new List<SceneLoader.SceneInfo>();
			IEnumerable<AssetData> enumerable = this.FindScenes();
			foreach (AssetData assetData in enumerable)
			{
				SceneLoader.SceneInfo sceneInfo = new SceneLoader.SceneInfo();
				sceneInfo.scene = assetData.name;
				sceneInfo.thumbnail = TextureLoader.LoadNewSprite(assetData.thumbnail, 100f, SpriteMeshType.Tight);
				sceneInfo.assetData = assetData;
				this.allScenes.Add(sceneInfo);
			}
			foreach (SceneLoader.SceneElement sceneElement in this.localScenes)
			{
				SceneLoader.SceneInfo sceneInfo2 = new SceneLoader.SceneInfo();
				sceneInfo2.scene = sceneElement.scene;
				sceneInfo2.thumbnail = sceneElement.thumbnail;
				this.allScenes.Add(sceneInfo2);
			}
		}
		return this.allScenes;
	}

	// Token: 0x0600163F RID: 5695 RVA: 0x0009078C File Offset: 0x0008E98C
	public void LoadScene(SceneLoader.SceneInfo scene)
	{
		this.loadingScene = scene;
		if (scene.assetData != null)
		{
			this.LoadAssetBundleScene(scene.assetData.path);
		}
		else
		{
			SceneManager.LoadScene(scene.scene);
		}
	}

	// Token: 0x06001640 RID: 5696 RVA: 0x000907C4 File Offset: 0x0008E9C4
	public void LoadSceneByName(string name)
	{
		List<SceneLoader.SceneInfo> sceneList = this.GetSceneList();
		foreach (SceneLoader.SceneInfo sceneInfo in sceneList)
		{
			if (sceneInfo.scene == name)
			{
				this.LoadScene(sceneInfo);
				return;
			}
		}
		Debug.LogError("The scene " + name + " couldn't be found.");
	}

	// Token: 0x06001641 RID: 5697 RVA: 0x00090850 File Offset: 0x0008EA50
	private IEnumerable<AssetData> FindScenes()
	{
		if (this.assetDataList != null)
		{
			return this.assetDataList;
		}
		this.assetDataList = new List<AssetData>();
		List<string> list = new List<string>();
		IOManager.AddIf(list, IOManager.GetApplicationDirectory(Path.Combine("Models", "Scenes")));
		IOManager.AddIf(list, IOManager.GetUserDirectory(Path.Combine("Models", "Scenes"), false));
		XmlResourceSettingReader xmlResourceSettingReader = new XmlResourceSettingReader(true);
		list.AddRange(xmlResourceSettingReader.ScenePaths);
		foreach (string dir in list)
		{
			this.RecursiveSearchOfAssets(dir);
		}
		this.assetDataList.Sort((AssetData a, AssetData b) => a.modified.CompareTo(b.modified));
		this.assetDataList.Reverse();
		return this.assetDataList;
	}

	// Token: 0x06001642 RID: 5698 RVA: 0x0009094C File Offset: 0x0008EB4C
	private static AssetData ReadAssetDataAtPath(string path, string dir)
	{
		string json = File.ReadAllText(path);
		Manifest manifest = JsonUtility.FromJson<Manifest>(json);
		AssetData assetData = new AssetData();
		assetData.name = manifest.name;
		assetData.dir = dir + "/";
		assetData.thumbnail = assetData.dir + manifest.thumbnail;
		assetData.data = assetData.dir + manifest.data;
		assetData.path = assetData.dir + manifest.asset;
		assetData.modified = File.GetLastAccessTime(path).Ticks;
		return assetData;
	}

	// Token: 0x06001643 RID: 5699 RVA: 0x000909E4 File Offset: 0x0008EBE4
	private void RecursiveSearchOfAssets(string dir)
	{
		string[] files = Directory.GetFiles(dir);
		foreach (string text in files)
		{
			if (text.EndsWith("manifest.json"))
			{
				AssetData assetData = SceneLoader.ReadAssetDataAtPath(text, dir);
				if (assetData != null)
				{
					this.assetDataList.Add(assetData);
					return;
				}
			}
		}
		string[] directories = Directory.GetDirectories(dir);
		foreach (string dir2 in directories)
		{
			this.RecursiveSearchOfAssets(dir2);
		}
	}

	// Token: 0x06001644 RID: 5700 RVA: 0x00090A74 File Offset: 0x0008EC74
	private void LoadAssetBundleScene(string assetPath)
	{
		string text = string.Empty;
		if (!this.loadedAssetBundles.ContainsKey(assetPath))
		{
			AssetBundle assetBundle = AssetBundle.LoadFromFile(assetPath);
			if (!(assetBundle != null))
			{
				return;
			}
			text = assetBundle.GetAllScenePaths()[0];
			this.loadedAssetBundles[assetPath] = text;
		}
		else
		{
			text = this.loadedAssetBundles[assetPath];
		}
		SceneManager.LoadScene(text);
	}

	// Token: 0x06001645 RID: 5701 RVA: 0x00090AE0 File Offset: 0x0008ECE0
	private void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
	{
		Debug.Log("Scene Loaded!");
		if (this.loadingScene != null && this.loadingScene.assetData != null)
		{
			Debug.Log(this.loadingScene.assetData.data);
			string json = File.ReadAllText(this.loadingScene.assetData.data);
			SceneData sceneData = JsonUtility.FromJson<SceneData>(json);
			foreach (MaterialData materialData in sceneData.materials)
			{
				this.shaderData[materialData.gameobject + materialData.material] = materialData.shader;
			}
			Renderer[] array = UnityEngine.Object.FindObjectsOfType<Renderer>();
			foreach (Renderer renderer in array)
			{
				foreach (Material material in renderer.materials)
				{
					string shaderName = this.SearchShaderName(renderer.name, material.name);
					ShaderLoader.SetShaderOnMaterial(material, shaderName);
				}
			}
			if (RenderSettings.skybox != null)
			{
				string text = this.SearchShaderName("Scene-Skybox", RenderSettings.skybox.name);
				if (text != string.Empty)
				{
					ShaderLoader.SetShaderOnMaterial(RenderSettings.skybox, text);
				}
				else
				{
					RenderSettings.skybox = this.defaultSkybox;
				}
			}
			Terrain[] array3 = UnityEngine.Object.FindObjectsOfType<Terrain>();
			foreach (Terrain terrain in array3)
			{
				TerrainData terrainData = terrain.terrainData;
				TreePrototype[] treePrototypes = terrainData.treePrototypes;
				foreach (TreePrototype treePrototype in treePrototypes)
				{
					Renderer component = treePrototype.prefab.GetComponent<Renderer>();
					foreach (Material material2 in component.sharedMaterials)
					{
						string shaderName2 = this.SearchShaderName(component.name, material2.name);
						ShaderLoader.SetShaderOnMaterial(material2, shaderName2);
					}
				}
				terrainData.RefreshPrototypes();
			}
			Camera[] array6 = UnityEngine.Object.FindObjectsOfType<Camera>();
			foreach (Camera camera in array6)
			{
				camera.gameObject.SetActive(false);
			}
			Vector3 position = Vector3.zero;
			Quaternion rotation = Quaternion.identity;
			PlayerSpawnPoint[] array8 = UnityEngine.Object.FindObjectsOfType<PlayerSpawnPoint>();
			Debug.Log("Spawn Points found: " + array8.Length);
			if (array8.Length > 0)
			{
				int num2 = UnityEngine.Random.Range(0, array8.Length);
				PlayerSpawnPoint playerSpawnPoint = array8[num2];
				position = playerSpawnPoint.transform.position;
				rotation = playerSpawnPoint.transform.rotation;
				GameController.startingSize = playerSpawnPoint.transform.localScale.y;
				foreach (PlayerSpawnPoint playerSpawnPoint2 in array8)
				{
					playerSpawnPoint2.gameObject.SetActive(false);
				}
			}
			if (UnityEngine.Object.FindObjectOfType<EventSystem>() == null)
			{
				GameObject gameObject = new GameObject("Event System");
				gameObject.AddComponent<EventSystem>();
				gameObject.AddComponent<StandaloneInputModule>();
			}
			GameObject gameObject2 = UnityEngine.Object.Instantiate<GameObject>(this.playerSpawn, position, rotation);
			gameObject2.AddComponent<PerformanceTuner>();
			Light[] array10 = UnityEngine.Object.FindObjectsOfType<Light>();
			foreach (Light light in array10)
			{
				if (light.type == LightType.Directional)
				{
					SimpleSunController.Sun = light.gameObject;
				}
			}
			if (this.cloudController != null)
			{
				foreach (Light light2 in array10)
				{
					if (light2.type == LightType.Directional)
					{
						UnityEngine.Object.Instantiate<GameObject>(this.cloudController).GetComponent<CloudController>();
						break;
					}
				}
			}
		}
		this.loadingScene = null;
	}

	// Token: 0x06001646 RID: 5702 RVA: 0x00090EE0 File Offset: 0x0008F0E0
	private string SearchShaderName(string gameobject, string material)
	{
		string key = gameobject + material.Replace(" (Instance)", string.Empty);
		if (this.shaderData.ContainsKey(key))
		{
			return this.shaderData[key];
		}
		Debug.LogError(string.Concat(new string[]
		{
			"The material ",
			material,
			" of ",
			gameobject,
			" is not on the list of scene shaders."
		}));
		return string.Empty;
	}

	// Token: 0x040018F2 RID: 6386
	public Material defaultSkybox;

	// Token: 0x040018F3 RID: 6387
	public GameObject playerSpawn;

	// Token: 0x040018F4 RID: 6388
	public GameObject cloudController;

	// Token: 0x040018F5 RID: 6389
	public List<SceneLoader.SceneElement> localScenes;

	// Token: 0x040018F6 RID: 6390
	private List<SceneLoader.SceneInfo> allScenes;

	// Token: 0x040018F7 RID: 6391
	private SceneLoader.SceneInfo loadingScene;

	// Token: 0x040018F8 RID: 6392
	private List<AssetData> assetDataList;

	// Token: 0x040018F9 RID: 6393
	private Dictionary<string, string> loadedAssetBundles;

	// Token: 0x040018FA RID: 6394
	private Dictionary<string, string> shaderData;

	// Token: 0x0200035D RID: 861
	[Serializable]
	public class SceneElement
	{
		// Token: 0x040018FC RID: 6396
		public string scene;

		// Token: 0x040018FD RID: 6397
		public Sprite thumbnail;
	}

	// Token: 0x0200035E RID: 862
	public class SceneInfo
	{
		// Token: 0x040018FE RID: 6398
		public string scene;

		// Token: 0x040018FF RID: 6399
		public Sprite thumbnail;

		// Token: 0x04001900 RID: 6400
		public AssetData assetData;
	}
}
